package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysStock;
import com.ruoyi.system.service.ISysStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/stock")
public class SysStockController extends BaseController
{
    private String prefix = "system/stock";
    @Autowired(required = false)
    private ISysStockService stockService;

    @RequiresPermissions("system:stock:view")
    @GetMapping()
    public String sku()
    {
        return prefix + "/stock";
    }

    @RequiresPermissions("system:stock:list")
    @RequestMapping("/list")
    @ResponseBody
    public List<SysStock> list(SysStock stock)
    {
        List<SysStock> stockList = stockService.selectStockList(stock);
        return stockList;
    }

    /**
     * 新增库存
     */
    @GetMapping("/add")
    public String add( ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增保存库存
     */
    @Log(title = "库存管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:stock:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysStock stock)
    {

        stock.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(stockService.insertStock(stock));
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{stockId}")
    public String edit(@PathVariable("stockId") Long stockId, ModelMap mmap)
    {
        SysStock stock = stockService.selectStockById(stockId);

        mmap.put("stock", stock);
        return prefix + "/edit";
    }

    /**
     * 保存
     */
    @Log(title = "库存管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:stock:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysStock stock)
    {

        stock.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(stockService.updateStock(stock));
    }


}
